package com.xsz.pda.net.udp;

import android.os.Build;

import java.io.IOException;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.DatagramChannel;

public class DatagramChannelBuilder {

    public static DatagramChannel openChannel() throws IOException {
        return DatagramChannel.open();
    }

    public static DatagramChannel bindChannel(SocketAddress local) throws IOException {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            return openChannel().bind(local);
        }
        throw new RuntimeException("Android 版本过低");
    }

    public static void sendMessage(DatagramChannel client, String msg, SocketAddress serverAddress) throws IOException {
        ByteBuffer buffer = ByteBuffer.wrap(msg.getBytes());
        client.send(buffer, serverAddress);
    }

}